/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.japo.java.matisse.samples;

import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JSlider;
import javax.swing.event.ChangeListener;
import org.japo.java.entities.Rotulo;

/**
 *
 * @author joanpaon
 */
public class ColorAsistido extends javax.swing.JFrame {

    Rotulo rotulo;
    String[] tipografias = GraphicsEnvironment.getLocalGraphicsEnvironment().
            getAvailableFontFamilyNames();

    public ColorAsistido() {
        initComponents();

        // Generar la entidad rotulo a partir del diseño
        generarModelo();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btgColor = new javax.swing.ButtonGroup();
        pnlColor = new javax.swing.JPanel();
        sldRojo = new javax.swing.JSlider();
        lblRojo = new javax.swing.JLabel();
        sldVerde = new javax.swing.JSlider();
        lblVerde = new javax.swing.JLabel();
        sldAzul = new javax.swing.JSlider();
        lblAzul = new javax.swing.JLabel();
        rbtFrente = new javax.swing.JRadioButton();
        lblFrente = new javax.swing.JLabel();
        rbtFondo = new javax.swing.JRadioButton();
        lblFondo = new javax.swing.JLabel();
        pnlArchivo = new javax.swing.JPanel();
        btnCargar = new javax.swing.JButton();
        btnGuardar = new javax.swing.JButton();
        lblMuestra = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Color asistido");
        setResizable(false);

        pnlColor.setBorder(javax.swing.BorderFactory.createTitledBorder("Color"));

        sldRojo.setMaximum(255);
        sldRojo.setValue(0);
        sldRojo.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sldRojoStateChanged(evt);
            }
        });

        lblRojo.setBackground(new java.awt.Color(255, 0, 0));
        lblRojo.setForeground(new java.awt.Color(255, 255, 255));
        lblRojo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblRojo.setText("0");
        lblRojo.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        lblRojo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        lblRojo.setOpaque(true);
        lblRojo.setPreferredSize(new java.awt.Dimension(34, 34));

        sldVerde.setMaximum(255);
        sldVerde.setValue(0);
        sldVerde.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sldVerdeStateChanged(evt);
            }
        });

        lblVerde.setBackground(new java.awt.Color(51, 204, 0));
        lblVerde.setForeground(new java.awt.Color(255, 255, 255));
        lblVerde.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblVerde.setText("0");
        lblVerde.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        lblVerde.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        lblVerde.setOpaque(true);
        lblVerde.setPreferredSize(new java.awt.Dimension(34, 34));

        sldAzul.setMaximum(255);
        sldAzul.setValue(0);
        sldAzul.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sldAzulStateChanged(evt);
            }
        });

        lblAzul.setBackground(new java.awt.Color(0, 0, 255));
        lblAzul.setForeground(new java.awt.Color(255, 255, 255));
        lblAzul.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblAzul.setText("0");
        lblAzul.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        lblAzul.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        lblAzul.setOpaque(true);
        lblAzul.setPreferredSize(new java.awt.Dimension(34, 34));

        btgColor.add(rbtFrente);
        rbtFrente.setSelected(true);
        rbtFrente.setText("Frente");
        rbtFrente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtFrenteActionPerformed(evt);
            }
        });

        lblFrente.setBackground(new java.awt.Color(0, 0, 0));
        lblFrente.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblFrente.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        lblFrente.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        lblFrente.setOpaque(true);
        lblFrente.setPreferredSize(new java.awt.Dimension(34, 34));

        btgColor.add(rbtFondo);
        rbtFondo.setText("Fondo");
        rbtFondo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtFondoActionPerformed(evt);
            }
        });

        lblFondo.setBackground(new java.awt.Color(255, 255, 255));
        lblFondo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblFondo.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        lblFondo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        lblFondo.setOpaque(true);
        lblFondo.setPreferredSize(new java.awt.Dimension(34, 34));

        javax.swing.GroupLayout pnlColorLayout = new javax.swing.GroupLayout(pnlColor);
        pnlColor.setLayout(pnlColorLayout);
        pnlColorLayout.setHorizontalGroup(
            pnlColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlColorLayout.createSequentialGroup()
                .addGroup(pnlColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(pnlColorLayout.createSequentialGroup()
                        .addComponent(sldRojo, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblRojo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlColorLayout.createSequentialGroup()
                        .addComponent(sldVerde, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblVerde, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlColorLayout.createSequentialGroup()
                        .addComponent(sldAzul, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblAzul, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlColorLayout.createSequentialGroup()
                        .addComponent(rbtFrente)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblFrente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlColorLayout.createSequentialGroup()
                        .addComponent(rbtFondo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblFondo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pnlColorLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {rbtFondo, rbtFrente, sldAzul});

        pnlColorLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {lblFondo, lblFrente});

        pnlColorLayout.setVerticalGroup(
            pnlColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlColorLayout.createSequentialGroup()
                .addGroup(pnlColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sldRojo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblRojo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sldVerde, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblVerde, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblAzul, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sldAzul, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rbtFrente)
                    .addComponent(lblFrente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rbtFondo)
                    .addComponent(lblFondo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        pnlColorLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {lblRojo, sldRojo});

        pnlColorLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {lblVerde, sldVerde});

        pnlColorLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {lblAzul, sldAzul});

        pnlColorLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {lblFrente, rbtFrente});

        pnlColorLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {lblFondo, rbtFondo});

        pnlArchivo.setBorder(javax.swing.BorderFactory.createTitledBorder("Archivo"));

        btnCargar.setText("Cargar");
        btnCargar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCargarActionPerformed(evt);
            }
        });

        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlArchivoLayout = new javax.swing.GroupLayout(pnlArchivo);
        pnlArchivo.setLayout(pnlArchivoLayout);
        pnlArchivoLayout.setHorizontalGroup(
            pnlArchivoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlArchivoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlArchivoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCargar, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        pnlArchivoLayout.setVerticalGroup(
            pnlArchivoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlArchivoLayout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(btnCargar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnGuardar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        lblMuestra.setBackground(new java.awt.Color(255, 255, 255));
        lblMuestra.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        lblMuestra.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblMuestra.setText("Texto de prueba");
        lblMuestra.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        lblMuestra.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        lblMuestra.setOpaque(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(lblMuestra, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnlColor, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnlArchivo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblMuestra, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlArchivo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        try {
            // Guarda los datos del rótulo al fichero
            exportarModelo();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error al guardar la configuración");
        }
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void btnCargarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCargarActionPerformed
        try {
            // Generar un modelo con los datos cargados
            importarModelo();

            // Aplicar el modelo al rótulo
            actualizarRotulo();

            // Aplicar el modelo al interfaz
            actualizarInterfaz();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error al cargar la configuración");
        }
    }//GEN-LAST:event_btnCargarActionPerformed

    private void sldRojoStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sldRojoStateChanged
        // Actualizar el color del rótulo en el modelo
        actualizarModelo();

        // Aplica el modelo al rótulo
        actualizarRotulo();

        // Actualiza la etiqueta de cantidad de color
        lblRojo.setText("" + sldRojo.getValue());

        // Actualiza la muestra de color
        if (rbtFrente.isSelected()) {
            lblFrente.setBackground(rotulo.getColorFrente());
        } else {
            lblFondo.setBackground(rotulo.getColorFondo());            
        }
    }//GEN-LAST:event_sldRojoStateChanged

    private void sldVerdeStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sldVerdeStateChanged
        // Actualizar el color del rótulo en el modelo
        actualizarModelo();

        // Aplica el modelo al rótulo
        actualizarRotulo();

        // Actualiza la etiqueta de cantidad de color
        lblVerde.setText("" + sldVerde.getValue());

        // Actualiza la muestra de color
        if (rbtFrente.isSelected()) {
            lblFrente.setBackground(rotulo.getColorFrente());
        } else {
            lblFondo.setBackground(rotulo.getColorFondo());            
        }
    }//GEN-LAST:event_sldVerdeStateChanged

    private void sldAzulStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sldAzulStateChanged
        // Actualizar el color del rótulo en el modelo
        actualizarModelo();

        // Aplica el modelo al rótulo
        actualizarRotulo();

        // Actualiza la etiqueta de cantidad de color
        lblAzul.setText("" + sldAzul.getValue());

        // Actualiza la muestra de color
        if (rbtFrente.isSelected()) {
            lblFrente.setBackground(rotulo.getColorFrente());
        } else {
            lblFondo.setBackground(rotulo.getColorFondo());            
        }
    }//GEN-LAST:event_sldAzulStateChanged

    private void rbtFrenteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtFrenteActionPerformed
        // Establece los selectores de color
        actualizarInterfaz();
    }//GEN-LAST:event_rbtFrenteActionPerformed

    private void rbtFondoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtFondoActionPerformed
        // Establece los selectores de color
        actualizarInterfaz();
    }//GEN-LAST:event_rbtFondoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ColorAsistido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ColorAsistido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ColorAsistido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ColorAsistido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ColorAsistido().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup btgColor;
    private javax.swing.JButton btnCargar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JLabel lblAzul;
    private javax.swing.JLabel lblFondo;
    private javax.swing.JLabel lblFrente;
    private javax.swing.JLabel lblMuestra;
    private javax.swing.JLabel lblRojo;
    private javax.swing.JLabel lblVerde;
    private javax.swing.JPanel pnlArchivo;
    private javax.swing.JPanel pnlColor;
    private javax.swing.JRadioButton rbtFondo;
    private javax.swing.JRadioButton rbtFrente;
    private javax.swing.JSlider sldAzul;
    private javax.swing.JSlider sldRojo;
    private javax.swing.JSlider sldVerde;
    // End of variables declaration//GEN-END:variables

    private void generarModelo() {
        String texto = lblMuestra.getText();
        Font fuente = lblMuestra.getFont();
        Color colorFrente = lblMuestra.getForeground();
        Color colorFondo = lblMuestra.getBackground();
        rotulo = new Rotulo(texto, fuente, colorFrente, colorFondo);
    }

    private void exportarModelo() throws Exception {
        final String ARCHIVO = "rotulo.txt";
        PrintWriter salida = null;
        String linea = "";

        // Texto del rótulo
        String texto = rotulo.getTexto();
        linea += texto + ",";

        // Tipografia de la fuente
        String tipografia = rotulo.getFuente().getFamily();
        linea += tipografia + ",";

        // Estilo de la fuente
        int estilo = rotulo.getFuente().getStyle();
        linea += estilo + ",";

        // Talla de la fuente
        int talla = rotulo.getFuente().getSize();
        linea += talla + ",";

        // Color de frente
        int frenteR = lblMuestra.getForeground().getRed();
        int frenteV = lblMuestra.getForeground().getGreen();
        int frenteA = lblMuestra.getForeground().getBlue();
        linea += frenteR + "," + frenteV + "," + frenteA + ",";

        // Color de fondo
        int fondoR = lblMuestra.getBackground().getRed();
        int fondoV = lblMuestra.getBackground().getGreen();
        int fondoA = lblMuestra.getBackground().getBlue();
        linea += fondoR + "," + fondoV + "," + fondoA;

        try {
            salida = new PrintWriter(new FileWriter("../" + ARCHIVO));
            salida.println(linea);
        } finally {
            if (salida != null) {
                salida.close();
            }
        }
    }

    private void importarModelo() throws Exception {
        final String ARCHIVO = "rotulo.txt";
        BufferedReader entrada = null;
        StringTokenizer st;
        String linea;

        try {
            entrada = new BufferedReader(new FileReader("../" + ARCHIVO));
            linea = entrada.readLine();
            st = new StringTokenizer(linea, ",");

            // Texto del rótulo
            String texto = st.nextToken();
            rotulo.setTexto(texto);

            // Tipografia de la fuente
            String tipografia = st.nextToken();

            // Estilo de la fuente
            int estilo = Integer.parseInt(st.nextToken());

            // Talla de la fuente
            int talla = Integer.parseInt(st.nextToken());

            // Fuente de la etiqueta
            Font f = new Font(tipografia, estilo, talla);
            rotulo.setFuente(f);

            // Color de frente
            int frenteR = Integer.parseInt(st.nextToken());
            int frenteV = Integer.parseInt(st.nextToken());
            int frenteA = Integer.parseInt(st.nextToken());
            Color colorFrente = new Color(frenteR, frenteV, frenteA);
            rotulo.setColorFrente(colorFrente);

            // Color de fondo
            int fondoR = Integer.parseInt(st.nextToken());
            int fondoV = Integer.parseInt(st.nextToken());
            int fondoA = Integer.parseInt(st.nextToken());
            Color colorFondo = new Color(fondoR, fondoV, fondoA);
            rotulo.setColorFondo(colorFondo);
        } finally {
            if (entrada != null) {
                entrada.close();
            }
        }
    }

    private void actualizarRotulo() {
        // Texto del rótulo
        String texto = rotulo.getTexto();
        lblMuestra.setText(texto);

        // Tipografia de la fuente
        String tipografia = rotulo.getFuente().getFamily();

        // Estilo de la fuente
        int estilo = rotulo.getFuente().getStyle();

        // Talla de la fuente
        int talla = rotulo.getFuente().getSize();

        // Fuente de la etiqueta
        Font f = new Font(tipografia, estilo, talla);
        lblMuestra.setFont(f);

        // Color de frente
        int frenteR = rotulo.getColorFrente().getRed();
        int frenteV = rotulo.getColorFrente().getGreen();
        int frenteA = rotulo.getColorFrente().getBlue();
        Color colorFrente = new Color(frenteR, frenteV, frenteA);
        lblMuestra.setForeground(colorFrente);

        // Color de fondo
        int fondoR = rotulo.getColorFondo().getRed();
        int fondoV = rotulo.getColorFondo().getGreen();
        int fondoA = rotulo.getColorFondo().getBlue();
        Color colorFondo = new Color(fondoR, fondoV, fondoA);
        lblMuestra.setBackground(colorFondo);
    }

    private void actualizarInterfaz() {
        if (rbtFrente.isSelected()) {
            actualizarSelectoresColor(rotulo.getColorFrente(), lblFrente);
        } else {
            actualizarSelectoresColor(rotulo.getColorFondo(), lblFondo);
        }
    }

    private void actualizarModelo() {
        // Determina el color modificado
        int rojo = sldRojo.getValue();
        int verde = sldVerde.getValue();
        int azul = sldAzul.getValue();
        Color c = new Color(rojo, verde, azul);

        // Registra el nuevo valor
        if (rbtFrente.isSelected()) {
            rotulo.setColorFrente(c);
        } else {
            rotulo.setColorFondo(c);
        }
    }

    private void actualizarSelectoresColor(Color color, JLabel muestra) {
        // Color rojo
        int rojo = color.getRed();
        actualizarDeslizador(sldRojo, rojo);
        lblRojo.setText("" + rojo);

        // Color verde
        int verde = color.getGreen();
        actualizarDeslizador(sldVerde, verde);
        lblVerde.setText("" + verde);

        // Color rojo
        int azul = color.getBlue();
        actualizarDeslizador(sldAzul, azul);
        lblAzul.setText("" + azul);

        // Muestra del color
        muestra.setBackground(color);
    }

    private void actualizarDeslizador(JSlider sldActual, int valor) {
        // Memoriza los gestores de cambio del deslizador
        ChangeListener[] lista = sldActual.getChangeListeners();

        // Elimina los gestores de cambio del deslizador
        for (int i = 0; i < lista.length; i++) {
            sldActual.removeChangeListener(lista[i]);
        }

        // Establece el valor deseado en el deslizador
        sldActual.setValue(valor);

        // Vuelve a registrar los gestores de cambio
        for (int i = 0; i < lista.length; i++) {
            sldActual.addChangeListener(lista[i]);
        }
    }
}
